package cs383.GradUnit;

import java.util.ArrayList;
import java.util.Random;

import android.os.Handler;
import android.util.Log;
import android.view.View;

public class cTable {
	
	private int dealerButton;
	
	private int numberOfActivePlayers;
	private int numberOfPlayersInHand;
	
	private int currentPot;
	private int minBet;
	
	private int currentBettingRound;
	private int roundNumber;
	
	private ArrayList<cPlayer> playerList;
	
	cTable(){
		minBet = 0;
		currentPot = 0;
		numberOfActivePlayers = 4;
		numberOfPlayersInHand = 0;
		roundNumber = 0;
		
		playerList = new ArrayList<cPlayer>();
		playerList.add(new cPlayer(1000));
		playerList.add(new cPlayer(1000));
		playerList.add(new cPlayer(1000));
		playerList.add(new cPlayer(1000));
		
		Random randomGenerator = new Random();
    	dealerButton = randomGenerator.nextInt(4);
	}
	void handInitialization(){
		numberOfPlayersInHand = numberOfActivePlayers;
		for(int i = 0; i < 4; i ++){
			if(playerList.get(i).inGame)
				playerList.get(i).inHand = true;
		}
		currentPot = 0;
		currentBettingRound = 1;
		roundNumber++;
	}
	int getCurrentRoundNumber(){
		return roundNumber;
	}
	int getBettingRound(){
		return currentBettingRound;
	}
	void incrementBettingRound(){
		currentBettingRound++;
	}
	void setMinBet(int amount){
		minBet = amount;
	}
	
	int getMinBet(){
		return minBet;
	}
	
	void addToPot(int bet){
		currentPot+=bet;
	}
	int getCurrentPot(){
		return currentPot;
	}
	int getDealerButton(){
		return dealerButton;
	}
	void moveDealerButton(){
    	numberOfPlayersInHand = numberOfActivePlayers;
    	
    	dealerButton++;
    	if(dealerButton > 3){
    		dealerButton = 0;
    	}
    	
    	int smallBlind = dealerButton + 1;
    	if(smallBlind > 3){
    		smallBlind = 0;
    	}
    	
    	int largeBlind = smallBlind + 1;
    	if(largeBlind > 3){
    		largeBlind = 0;
    	}
    	
    	Log.d("moveDealerButton","Dealer button on Player "+dealerButton);
    	Log.d("moveDealerButton", "Small Blind: "+smallBlind);
    	Log.d("moveDealerButton", "Large Blind: "+largeBlind);
    	takeBlinds(smallBlind,largeBlind);
    	
    	
    	
    }
	private void takeBlinds(int smallBlind, int largeBlind){
    	playerList.get(smallBlind).takeChips(minBet);
    	playerList.get(largeBlind).takeChips(2*minBet);
    	
    	currentPot += (minBet + 2*minBet);
    	
    	
    }
	void placeHumanBet(int chipAmount){
		playerList.get(0).takeChips(chipAmount);
		currentPot+=chipAmount;
	}
	void awardPot(){
		playerList.get(dealerButton).awardChips(currentPot);
		//currentPot = 0;
		
	}
	
	int getPlayerChipAmount(int playerNumber){
		return playerList.get(playerNumber).getChipAmount();
	}
	
	int getNumberOfActivePlayers(){
		return numberOfActivePlayers;
	}
	int getNumberOfPlayersInHand(){
		return numberOfPlayersInHand;
	}
	
	int makePlayerDecisions(){
		int playersThatActed = 0;
		for(int i = dealerButton+1; i < 4; i++ ){
			int playerToAct = i % numberOfPlayersInHand;
			if(playerToAct == 0)
				break;
			getPlayerDecision(playerToAct);
			playersThatActed++;
		}
		return playersThatActed;
		
	}
	
	double getPlayerDecision(int playerNum){
		final int num = playerNum;
		/*
		Handler mHandler = new Handler();
    	Runnable playerDecision = new Runnable() {
    	    public void run() {
    	    	Log.d("decision",  "Player " + num + " making decision");
    	    	Random randomGen = new Random();
    	    	double playerChoice = cBehaviorModule.makeDecision(randomGen.nextDouble());  //this will be percent change to win for player hand
    	    	
    	    	if(playerChoice == cBehaviorModule.CALL){
    	    		
    	    		
    	    	}
    	    	else if(playerChoice == cBehaviorModule.FOLD){
    	    		
    	    	}
    	    	else{
    	    		
    	    	}
    	    }
    	};
    	mHandler.postDelayed(playerDecision, 2000);
    	*/
		Random randomGen = new Random();
    	double playerChoice = cBehaviorModule.makeDecision(randomGen.nextDouble());  //this will be percent change to win for player hand
    	
    	if(playerChoice == cBehaviorModule.CALL){
    		
    		return playerChoice;
    	}
    	else if(playerChoice == cBehaviorModule.FOLD){
    		
    		return playerChoice;
    	}
    	else{
    		return playerChoice;
    	}
		
	}
	
	boolean checkPlayerInHand(int playerNum){
		if(playerList.get(playerNum).inHand)
			return true;
		else
			return false;
	}
	
	void fold(int playerNum){
		playerList.get(playerNum).inHand = false;
	}
	
	
}
